package com.purong.purong.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.purong.purong.R;
import com.purong.purong.bean.QuestionBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wenjiezeng on 2017/11/2.
 */
public class LoanDetQuestionAdapter extends RecyclerView.Adapter<LoanDetQuestionAdapter.ViewHolder> {
    private List<QuestionBean> questionList =new ArrayList<>();
    private Context mContext;
    private OnLoanQuestionListener mListener;
    public LoanDetQuestionAdapter(Context context){
        this.mContext=context;
    }
    public void setData(List<QuestionBean> List) {

        this.questionList = List;
    }
    public void setOnLoanQuestionListener(OnLoanQuestionListener listener){
        this.mListener=listener;
    }
    @Override
    public LoanDetQuestionAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.loan_det_question_item, parent, false);
        // set the view's size, margins, paddings and item_circle parameters
        ViewHolder vh = new ViewHolder(v);

        return vh;
    }

    @Override
    public void onBindViewHolder(LoanDetQuestionAdapter.ViewHolder holder, final int position) {
        if (questionList.get(position).getQuestion_content()!=null){
            int p=questionList.get(position).getQuestion_content().lastIndexOf("\\n");
            if (p!=-1){
                holder.questContent.setText(questionList.get(position).getQuestion_content().substring(0,p));
            }else {
                holder.questContent.setText(questionList.get(position).getQuestion_content());
            }
        }
        holder.mainLay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener!=null){
                    mListener.onQuestionClick(questionList.get(position));
                }
            }
        });
//        if (position==questionList.size()-1){
//            holder.cutLine.setVisibility(View.INVISIBLE);
//        }else {
//            holder.cutLine.setVisibility(View.VISIBLE);
//
//        }

    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        //绑定每个CircleItem的控件引用到ViewHolder
        private TextView questContent;
        private View cutLine;
        private LinearLayout mainLay;
        public ViewHolder(View v) {
            super(v);
            cutLine=(View)v.findViewById(R.id.cut_line) ;
            questContent=(TextView)v.findViewById(R.id.question);
            mainLay=(LinearLayout)v.findViewById(R.id.main_lay);

        }
    }
    @Override
    public int getItemCount() {
        return questionList.size();
    }

    public interface OnLoanQuestionListener{
        void onQuestionClick(QuestionBean bean);
    }
}
